package com.cll.pmis.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.cll.pmis.common.Constants;
import com.cll.pmis.common.exception.TransactionException;
import com.cll.pmis.entity.Input;
import com.cll.pmis.entity.InputDrug;
import com.cll.pmis.mapper.InputMapper;
import com.cll.pmis.service.IInputDrugService;
import com.cll.pmis.service.IInputService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author chenliangliang
 * @since 2018-06-18
 */
@Service
public class InputServiceImpl extends ServiceImpl<InputMapper, Input> implements IInputService {

    private IInputDrugService inputDrugService;

    protected InputServiceImpl(IInputDrugService inputDrugService){
        this.inputDrugService=inputDrugService;
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void createInput(String username, Input input) {

        //先创建入库单
        Input in=new Input();
        in.setStatus(Constants.INPUT_STATUS_UNCHECK);
        in.setProvider(input.getProvider());
        in.setUsername(username);
        in.setType(input.getType());
        in.setRemark(input.getRemark());
        in.setCreateTime(new Date());
        in.setUpdateTime(new Date());

        if (!insert(in)){
            throw new TransactionException("创建入库单失败");
        }
        List<InputDrug> inputDrugs=input.getList();
        inputDrugs.forEach(it->it.setInputId(in.getId()));

        if (!inputDrugService.insertBatch(inputDrugs)){
            throw new TransactionException("插入入库药品异常");
        }

    }

}
